//index.js
//获取应用实例
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({
  data: {
    active:0,
    listArr:[],
    topicArr:[],
    stop:false,
    page: {//话题详情
      p: 0,
      psize: 10
    },
    focuspage: {//话题详情
      p: 0,
      psize: 10
    },
    hei:0,
    scrollTop:0,
    invitationArr:[],
    focusArr:[]//关注
  },
  onLoad: function (options) {
    // if (!wx.getStorageSync("user")){
    //   wx.redirectTo({
    //     url: '../logs/logs'
    //   })
    // }else{
      this.init();
        wx.getSystemInfo({
        success: res => {
          let windowHeight = (res.windowHeight * (750 / res.windowWidth));
          this.setData({
            hei: windowHeight - 142 - 32
          })
        }
      })
    //}
    
  
  },
  down() {
    if(this.data.active == 0){
      wx.showLoading({
        title: '加载中',
        success: () => {
          this.setData({
            'page.p': this.data.page.p + 1
          })
          this.getPostList()

        }
      })
    }else{
      
      this.setData({
        'focuspage.p': this.data.focuspage.p + 1
      })
      this.focusInit()
    }
    
  },
  up() {
    let refresh = true
    if(this.data.active == 0){
      this.setData({
        'page.p': 0
      })
      this.init(refresh)
    }else{
      this.setData({
        focusArr: []
      })
      this.setData({
        'focuspage.p': 0
      })
      this.focusInit(refresh)
    }
    wx.stopPullDownRefresh();  
  },
  init:function(refresh){//初始函数 获取
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Public/getRecmdDog',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            if (res.data.success == '1' && res.data.data) {
              let listArr = res.data.data.map(v=>{
                if(v.dog_name.length>2){
                  v.dog_name = v.dog_name.slice(0,2) +'...'
                }
                return v
              }) 
              this.setData({
                listArr: res.data.data
              })
                
              wx.request({
                url: base_url + '/Public/getRecmdTopic',
                data: { 'is_all': 0 },
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                  if (res.data.success == '1' && res.data.data) {
                    this.setData({
                      topicArr: res.data.data
                    })
                    this.getPostList(refresh)
                  }
                },
                fail(){
                  wx.showToast({
                    title: '加载失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            }

          },
          fail(){
            wx.showToast({
              title: '加载失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
        
        
      },
      fail(){
        wx.showToast({
          title: '加载失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
    
  },
  imgYu(e){
    let list = e.currentTarget.dataset.list,
      img = e.currentTarget.dataset.img;
    wx.previewImage({
      current:img,
      urls:list
    })
  },
  gzimg(e){
    let list=[],
      imglist = e.currentTarget.dataset.list,
      img = e.currentTarget.dataset.img;
    for(let val of imglist){
      list.push(val.photo_url)
    }
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // onReady: function () {  //创建视频上下文对象
  //   this.videoContext = wx.createVideoContext('myVideo')
  // },
  videoplay(e){
    let index = e.currentTarget.dataset.index;
    let arr = this.data.invitationArr;
    for(let val of arr){
      if (val.video_url != null){
        val.playing=false
      }
    }
    if(arr[index].playing){
      arr[index].playing=false
      //this.videoContext.stop()
    }else{
      arr[index].playing = true
      //this.videoContext.play()
    }
    this.setData({
      invitationArr: arr
    })
   
   
  },
  getPostList(refresh = false){
    wx.request({
      url: base_url + '/Post/getList',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'p': this.data.page.p,
        'psize': this.data.page.psize
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        if (res.data.success == '1') {
          if (res.data.data.list.length == 0){
            wx.showToast({
              title: '暂无更多数据',
              icon: 'none',
              duration: 2000
            })
            return
          }
          let arr = refresh ? res.data.data.list : this.data.invitationArr.concat(res.data.data.list)
          for(let val of arr){
            if (val.video_url!=null){
                val.playing=false;
            }
          }
          this.setData({
            invitationArr: arr
          })
          wx.hideLoading();
          refresh == true && wx.stopPullDownRefresh();
        }
      }
    })
  },
  moreList: function(e){
    let id = e.currentTarget.dataset.id;
    if(id){
      wx.navigateTo({
        url: '../dcDetails/dcDetails?id='+id
      }) 
    }else{
      wx.navigateTo({
        url: '../list/list'
      })
    }
  },
  topic:function(e){
    wx.navigateTo({
      url: '/pages/topicDetail/topicDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  detail:function(e){
    if (e.currentTarget.dataset.type == 10){
      wx.navigateTo({
        url: '../saledetail/saledetail?id=' + e.currentTarget.dataset.id
      })
    }else{
      wx.navigateTo({
        url: '../details/details?id=' + e.currentTarget.dataset.id
      })
    }
   
  },
  todog(e){
    let types = e.currentTarget.dataset.type,
      post_type = e.currentTarget.dataset.post;
    if(types==1){
      wx.navigateTo({
        url: '../dcDetails/dcDetails?id=' + e.currentTarget.dataset.id
      })
    }
    else{
      if (post_type==10){
        wx.navigateTo({
          url: '../saledetail/saledetail?id=' + e.currentTarget.dataset.pid
        })
      }else{
        wx.navigateTo({
          url: '../details/details?id=' + e.currentTarget.dataset.pid
        })
      }
    }
  },
  moreTopic:function(){
    wx.navigateTo({
      url: '../topics/topics'
    })
  },
  handleZan:function(e){
    let i = e.currentTarget.dataset.id;
    util.zan({
      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'post_id':e.currentTarget.dataset.postid
    },(res) => {
        let is_zan = 'invitationArr[' + i + '].is_zan';
        let zan_num = 'invitationArr[' + i + '].zan_num';
        let num = Number(this.data.invitationArr[i].zan_num);
        this.setData({
          [is_zan]: this.data.invitationArr[i].is_zan == 0 ? 1 : 0,
          [zan_num]: this.data.invitationArr[i].is_zan == 0 ? num+1 : num-1
        })
    })
  },
  navSel(e){
    this.setData({
      scrollTop:0
    })
    this.setData({
      active:e.currentTarget.dataset.type
    })
    if(this.data.active == 1){//关注
      this.setData({
        stop:false
      })
      this.focusInit();
    }
    
  },
  handletouchmove(event){
    let arr = this.data.invitationArr;
    // for (let val of arr) {
    //   if(val.playing){
    //     if (val.video_url != null) {
    //       val.playing = false
    //     }
    //   }
    // }
    for (let val of arr) {
      if (val.video_url != null) {
        val.playing = false
      }
    }
    this.setData({
      invitationArr: arr
    })
  },
  focusInit(refresh = false){
    if(this.data.stop == true){
      return
    }
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Users/getFollwong',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.focuspage.p,
            'psize': this.data.focuspage.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              if (res.data.data.length == 0){
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
                this.setData({
                  stop:true
                })
                return
              }
              let focusArr = this.data.focusArr.concat(res.data.data)
              this.setData({
                focusArr:focusArr
              })
            }
              refresh == true && wx.stopPullDownRefresh();
              wx.hideLoading();
          }
        })
      }
    })
  }
})
